package com.robot.backend.service.impl.record;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.robot.backend.mapper.RobotMapper;
import com.robot.backend.mapper.UserMapper;
import com.robot.backend.pojo.Record;
import com.robot.backend.pojo.User;
import com.robot.backend.service.record.recordCRUDService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.robot.backend.mapper.RecordMapper;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class recordCRUDServiceImpl implements recordCRUDService{

    @Autowired
    RecordMapper recordMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    RobotMapper robotMapper;
    @Override
    public boolean addRecord(int aid, int bid, int abot, int bbot, String loser, int[] asteps, int[] bsteps,
                             int[][] map, int asx, int asy, int bsx, int bsy,int winScore,int loseScore) {

        Date date = new Date();
        Record record = new Record();
        record.setAid(aid);
        record.setBid(bid);
        record.setAbot(abot);
        record.setBbot(bbot);
        record.setLoser(loser);
        record.setAsteps(Arrays.toString(asteps));
        record.setBsteps(Arrays.toString(bsteps));
        record.setAsx(asx);
        record.setAsy(asy);
        record.setBsx(bsx);
        record.setBsy(bsy);
        record.setMap(Arrays.deepToString(map));
        record.setCreatetime(date);
        record.setWinscore(winScore);
        record.setLosescore(loseScore);
        recordMapper.insert(record);


        User winner = null;
        if(loser.equals("all")) {
            User p1 = userMapper.selectById(aid);
            User p2 = userMapper.selectById(bid);
            p1.setContestn(p1.getContestn() + 1);
            p2.setContestn(p2.getContestn() + 1);
            userMapper.updateById(p1);
            userMapper.updateById(p2);
            return true;
        }
        User loserT = userMapper.selectById(Integer.parseInt(loser));
        if(aid == Integer.parseInt(loser)) winner = userMapper.selectById(bid);
        else winner = userMapper.selectById(aid);
        loserT.setScore(loserT.getScore()+loseScore);
        winner.setScore(winner.getScore()+winScore);
        loserT.setContestn(loserT.getContestn() + 1);
        winner.setContestn(winner.getContestn() + 1);
        winner.setWinn(winner.getWinn() + 1);
        userMapper.updateById(loserT);
        userMapper.updateById(winner);
        return true;
    }

    @Override
    public JSONObject getRecordById(int id, int page, int sizes) {
        if(StpUtil.getLoginIdAsInt() != id)
            return null;
        else {
            QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("aid",id).or().eq("bid",id).orderByDesc("createtime");
            IPage<Record> recordIPage = new Page<>(page,sizes);
            List<Record> records = recordMapper.selectPage(recordIPage,queryWrapper).getRecords();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("record",records);
            jsonObject.put("length",recordMapper.selectCount(queryWrapper));
            return jsonObject;
        }
    }

    @Override
    public JSONObject getAllRecord(int page, int sizes) {
        IPage<Record> iPage = new Page<>(page,sizes);
        QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("createtime");
        List<Record> records = recordMapper.selectPage(iPage,queryWrapper).getRecords();
        JSONObject jsonObject = new JSONObject();
        class temp{
            public Integer id;
            public Integer aid;
            public String aName;
            public Integer abot;
            public String abotName;
            public Integer asx;
            public Integer asy;
            public Integer bid;
            public String bName;
            public Integer bbot;
            public String bbotName;
            public Integer bsx;
            public Integer bsy;
            public String asteps;
            public String bsteps;
            public String map;
            public String loser;
            public String loserName;
            public Integer winscore;
            public Integer losescore;
            @JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8")
            public Date createtime;
        }
        List<temp> list = new ArrayList<>();
        for(Record i :records){
            User user1 = userMapper.selectById(i.getAid());
            User user2 = userMapper.selectById(i.getBid());
            String abotName = "";
            if(i.getAbot() == 0) abotName = "亲自上阵";
            else if(robotMapper.selectById(i.getAbot()) == null) abotName = "已删除";
            else abotName = robotMapper.selectById(i.getAbot()).getTitle();
            String bbotName = "";
            if(i.getBbot() == 0) abotName = "亲自上阵";
            else if(robotMapper.selectById(i.getBbot()) == null) bbotName = "已删除";
            else bbotName = robotMapper.selectById(i.getBbot()).getTitle();
            String loserName = "";
            if("all".equals(i.getLoser()) || i.getLoser().isEmpty() )
                loserName = "平局";
            else if(userMapper.selectById(Integer.parseInt(i.getLoser())) == null) loserName="已删除";
                else loserName = userMapper.selectById(Integer.parseInt(i.getLoser())).getName();
            temp temp = new temp();
            temp.id = i.getId();
            temp.aid = i.getAid();
            temp.asx = i.getAsx();
            temp.asy = i.getAsy();
            temp.aName = user1.getName();
            temp.bName =user2.getName();
            temp.bid = i.getBid();
            temp.bsx = i.getBsx();
            temp.bsy = i.getBsy();
            temp.abotName = abotName;
            temp.bbotName = bbotName;
            temp.loserName = loserName;
            temp.asteps =i.getAsteps();
            temp.bsteps = i.getBsteps();
            temp.map = i.getMap();
            temp.winscore = i.getWinscore();
            temp.losescore = i.getLosescore();
            temp.createtime = i.getCreatetime();
            temp.loser = i.getLoser();
            list.add(temp);
        }
        jsonObject.put("record",list);
        jsonObject.put("length",recordMapper.selectCount(null));
        return jsonObject;
    }
}
